package com.kkd.kkdapp.db.user.model;

import android.content.ContentValues;

import com.google.gson.Gson;
import com.kkd.kkdapp.db.CurUserModel;
import com.kkd.kkdapp.db.annotate.EADBField;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldMode;
import com.kkd.kkdapp.db.annotate.EADBField.EADBFieldType;
import com.kkd.kkdapp.db.base.EABaseModel;
import com.kkd.kkdapp.db.user.KkdDBHelper;
import com.kkd.kkdapp.http.entity.Privilege;
import com.kkd.kkdapp.http.entity.PrivilegeData;

public class PrivilegeModel extends EABaseModel {

	private static final String TABLE_NAME = PrivilegeModel.class
			.getSimpleName();

	// 用户ID
	@EADBField(mode = EADBFieldMode.NotNull, type = EADBFieldType.Text)
	public String customerId;

	// 客户特权详情
	@EADBField(type = EADBFieldType.Text)
	public String PrivilegeData;

	// 拥有特权的个数
	@EADBField(type = EADBFieldType.Integer)
	public int isHadPrivilege = 0;

	// 是否显示过特权提醒 isHadPrivilege>isShown 时即显示
	@EADBField(type = EADBFieldType.Integer)
	public int isShown = 0;

	public static long insertOrUpdate(PrivilegeData data) {
		if(CurUserModel.getCurUser()==null)
			return -1;
		PrivilegeModel model = KkdDBHelper.getInstance().queryTopOne(
				PrivilegeModel.class, " customerId=? ",
				new String[] { CurUserModel.getCurUser().customerId });
		Gson gson = new Gson();
		int isHadPrivilege = 0;
		for (Privilege i : data.getDataList()) { //遍历一下特权列表，看看是否有特权
			if (i.getStatus() == 1) {
				isHadPrivilege++;
			}
		}
		if (model != null) {
			ContentValues values = new ContentValues();
			values.put("PrivilegeData", gson.toJson(data));
			values.put("isHadPrivilege", isHadPrivilege);
			return KkdDBHelper.getInstance().update(TABLE_NAME, values,
					" customerId=? ",
					new String[] { CurUserModel.getCurUser().customerId });
		} else {
			PrivilegeModel newModel = new PrivilegeModel();
			newModel.customerId = CurUserModel.getCurUser().customerId;
			newModel.PrivilegeData = gson.toJson(data);
			newModel.isHadPrivilege = isHadPrivilege;
			return KkdDBHelper.getInstance().saveBindId(newModel);
		}

	}

	public static boolean updateShownById() {
		if(CurUserModel.getCurUser()==null)
			return false;
		PrivilegeModel newModel = KkdDBHelper.getInstance().queryTopOne(
				PrivilegeModel.class, " customerId=? ",
				new String[] { CurUserModel.getCurUser().customerId });
		if(newModel!=null){
			ContentValues values = new ContentValues();
			values.put("isShown", newModel.isHadPrivilege);
			return KkdDBHelper.getInstance().update(TABLE_NAME, values,
					" customerId=? ",
					new String[] { CurUserModel.getCurUser().customerId }) > 0;
		}
		return false;
	}

	/**
	 * findMyPrivilegeData：
	 * 获取当前用户的特权详情信息
	 * @author askzhao
	 * @return
	 */
	public static PrivilegeData findMyPrivilegeData() {
		if(CurUserModel.getCurUser()==null)
			return null;
		PrivilegeModel newModel = KkdDBHelper.getInstance().queryTopOne(
				PrivilegeModel.class, " customerId=? ",
				new String[] { CurUserModel.getCurUser().customerId });
		if (newModel != null) {
			Gson gson = new Gson();
			PrivilegeData data = gson.fromJson(newModel.PrivilegeData,
					PrivilegeData.class);
			return data;
		} else {
			return null;
		}
	}
	
	
	public static PrivilegeModel getPrivilegeModel() {
		if(CurUserModel.getCurUser()==null)
			return null;
		PrivilegeModel newModel = KkdDBHelper.getInstance().queryTopOne(
				PrivilegeModel.class, " customerId=? ",
				new String[] { CurUserModel.getCurUser().customerId });
		return newModel;
	}

}
